package com.train.course.controller;

import com.train.common.annotation.Anonymous;
import com.train.common.core.controller.BaseController;
import com.train.common.core.domain.AjaxResult;
import com.train.common.core.page.TableDataInfo;
import com.train.course.domain.VideoCourse;
import com.train.course.dto.QueryDTO;
import com.train.course.dto.VideoCourseDTO;
import com.train.course.service.IVideoCourseService;
import com.train.course.service.VideoCourseCategoryService;
import com.train.course.vo.QueryVideoCourseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 课程管理 控制器
 */
@RestController
@RequestMapping("/course")
public class VideoCourseController extends BaseController {
    @Autowired
    private IVideoCourseService videoCourseService;

    @Autowired
    private VideoCourseCategoryService videoCourseCategoryService;

    /**
     * 查询课程列表
     */
    @GetMapping("/list")
    public TableDataInfo list(VideoCourse videoCourse) {
        startPage();
        List<VideoCourse> list = videoCourseService.selectVideoCourseList(videoCourse);
        return getDataTable(list);
    }

    /**
     * 获取课程详细信息（包含标签）
     */
    @Anonymous
    @GetMapping(value = "/{courseId}")
    public Object getInfo(@PathVariable("courseId") Long courseId) {
        return videoCourseService.selectVideoCourseDetailById(courseId);
    }

    /**
     * 新增课程
     */
    @Anonymous
    @PostMapping
    public Object add(@RequestBody VideoCourse videoCourse) {
        int rows = videoCourseService.insertVideoCourse(videoCourse);
        if (rows > 0) {
            // MyBatis 会将生成的 courseId 回填到 videoCourse 对象中
            Map<String, Object> data = new HashMap<>();
            data.put("courseId", videoCourse.getCourseId());
            return AjaxResult.success("操作成功", data);
        } else {
            return AjaxResult.error();
        }
    }

    /**
     * 新增课程（包含分类和标签）
     */
    @Anonymous
    @PostMapping("/addWithTags")
    public Object addWithTags(@Validated @RequestBody VideoCourseDTO videoCourseDTO) {
        return toAjax(videoCourseService.insertVideoCourseWithTags(videoCourseDTO));
    }

    /**
     * 修改课程
     */
    @PutMapping
    public Object edit(@RequestBody VideoCourse videoCourse) {
        return toAjax(videoCourseService.updateVideoCourse(videoCourse));
    }

    /**
     * 修改课程（包含分类和标签）
     */
    @Anonymous
    @PutMapping("/updateWithTags")
    public Object updateWithTags(@Validated @RequestBody VideoCourseDTO videoCourseDTO) {
        return toAjax(videoCourseService.updateVideoCourseWithTags(videoCourseDTO));
    }

    /**
     * 删除课程
     */
    @DeleteMapping("/{courseIds}")
    public Object remove(@PathVariable Long[] courseIds) {
        return toAjax(videoCourseService.deleteVideoCourseByIds(courseIds));
    }

    /**
     * 下滑查询全部课程列表
     */

    @Anonymous
    @PostMapping("/queryAll")
    public QueryVideoCourseVO queryAllCourse(@RequestBody QueryDTO queryDTO) {
        return videoCourseCategoryService.queryAllCourse(queryDTO);
    }

    /**
     * 根据排序类型查询课程列表(点赞数/收藏数降序)
     * sortType: like(点赞数降序) 或 collect(收藏数降序)
     */
    @Anonymous
    @PostMapping("/queryBySort")
    public QueryVideoCourseVO queryCourseBySort(@RequestBody QueryDTO queryDTO) {
        return videoCourseCategoryService.queryCourseBySort(queryDTO);
    }
}